package org.youyee.game.display
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Transform;
	
	import org.youyee.game.utils.bitmapCacheUtils.BitmapCacheClipData;
	import org.youyee.game.utils.bitmapCacheUtils.BitmapCacheFrame;
	
	/**
	 * 用于显示位图化后的MovieClip 
	 * @author youyeelu
	 * 
	 */	
	public class BitmapCacheClipViewer extends Sprite
	{
		private var _bitmapCacheClipData:BitmapCacheClipData;
		private var _autoReplay:Boolean;
		private var _currentFrame:int;
		private var _totalFrames:int;
		
		/**
		 * 用于显示的帧 
		 */		
		public var frameViewer:BitmapCacheFrameViewer;
		
		/**
		 * 构造函数 
		 * @param clipData 显示数据
		 * @param autoReplay 是否循环播放
		 * 
		 */		
		public function BitmapCacheClipViewer(clipData:BitmapCacheClipData=null, autoReplay:Boolean=true)
		{
			if (clipData)
			{
				this.bitmapCacheClipData = clipData;
			}
			
			this.autoReplay = autoReplay;
			frameViewer = new BitmapCacheFrameViewer(true);
			addChild(frameViewer);
		}
		
		/**
		 * 设置当前是否可以自动重播 
		 * @param value
		 * 
		 */		
		public function set autoReplay(value:Boolean):void
		{
			this._autoReplay = value;
		}
		
		/**
		 * 获取当前是否自动重播 
		 * 
		 */		
		public function get autoReplay():Boolean
		{
			return this._autoReplay;
		}
		
		/**
		 * 设置当前显示的数据 
		 * @param value
		 * 
		 */		
		public function set bitmapCacheClipData(value:BitmapCacheClipData):void
		{
			_bitmapCacheClipData = value;
			_totalFrames = (_bitmapCacheClipData.frames.length - 1) * _bitmapCacheClipData.numJumpFrames;
			var t:Transform = this.transform;
			t.matrix = value.matrix;
			this.transform = t;
			
			gotoAndStop(1);
		}
		
		/**
		 * 获取当前显示的数据 
		 * @return 
		 * 
		 */		
		public function get bitmapCacheClipData():BitmapCacheClipData
		{
			return this._bitmapCacheClipData;
		}
		
		/**
		 * 获取当前的帧数 
		 * @return 
		 * 
		 */		
		public function get currentFrame():int
		{
			return this._currentFrame + 1;
		}
		
		/**
		 * 获取总共的帧数 
		 * @return 
		 * 
		 */		
		public function get totalFrames():int
		{
			return this._totalFrames + 1;
		}
		
		/**
		 * 播放 
		 * 
		 */		
		public function play():void
		{
			this.totalFrames > 1 && this.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
		}
		
		/**
		 * 暂停 
		 * 
		 */		
		public function pause():void
		{
			this.removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
		}
		
		private function handleEnterFrame(e:Event):void
		{
			if (_currentFrame < _totalFrames)
			{
				gotoAndStop(_currentFrame + 2);
			}
			else  if (this._currentFrame == this._totalFrames)
			{
				if (_autoReplay)
				{
					gotoAndStop(1);
				}
				else
				{
					pause();
				}
			}
		}
		
		/**
		 * 跳到某帧开始播放 
		 * @param frameIndex
		 * 
		 */		
		public function gotoAndPlay(frameIndex:int):void
		{
			this.gotoAndStop(frameIndex);
			this.play();
		}
		
		/**
		 * 跳到并停在某帧 
		 * @param frameIndex 帧数
		 * @return 是否执行成功
		 * 
		 */		
		public function gotoAndStop(frameIndex:int):Boolean
		{
			if (!_bitmapCacheClipData)
			{
				return false;
			}
			
			var index:int = frameIndex - 1;
			
			if (index >=0 && index <= _totalFrames)
			{
				index = Math.floor(index / _bitmapCacheClipData.numJumpFrames);
				_currentFrame = frameIndex - 1;
			}
			else
			{
				return false;
			}
			
			var f:BitmapCacheFrame = _bitmapCacheClipData.frames[index] as BitmapCacheFrame;
			
			if (f)
			{
				frameViewer.bitmapCacheFrame = f;
				this.frameViewer.visible = true;
			}
			
			return true;
		}
		
		public function clear():void
		{
			this.frameViewer.visible = false;
		}
		
		/**
		 * 销毁 
		 * 
		 */		
		public function destroy():void
		{
			this._bitmapCacheClipData = null;
			this.pause();
		}
	}
}